﻿using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infastruucture;
using BasicData.Write.API.application.Command.User;
using MediatR;

namespace BasicData.Write.API.application.CommandHandler.User
{
    public class RoleStatusCommandHandler : IRequestHandler<RoleStatusCommand, ApiResult<object>>
    {
        private readonly IBaseRepository<RoleModel> roleRepo;

        public RoleStatusCommandHandler(IBaseRepository<RoleModel> roleRepo)
        {
            this.roleRepo = roleRepo;
        }
        public Task<ApiResult<object>> Handle(RoleStatusCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> res = new ApiResult<object>();
            RoleModel role = roleRepo.GetByModel(request.RoleId);
            role.Status = !role.Status;
            int count = roleRepo.Update(role);
            if (count > 0)
            {
                res.Code = BaseEnums.Success;
                res.Msg = "角色状态修改成功 ";
                res.Data = role;
            }
            else
            {
                res.Code = BaseEnums.Failed;
                res.Msg = "角色状态修改失败 ";
                res.Data = role;
            }
            return Task.FromResult(res);
        }
    }
}
